package com.base.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.entity.FeeRecord;
import com.base.entity.Room;
import com.base.service.IFeeRecordService;
import com.base.service.IRoomService;
import com.base.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IFeeRecordService feeRecordService;

    @Autowired
    private IRoomService roomService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/temp")
    public String temp() {
        return "temp";
    }

    @GetMapping("/user_manage")
    public String user_manage(Model model) {
        model.addAttribute("users", userService.list());
        return "user_manage";
    }

    @GetMapping("/room_manage")
    public String room_manage(Model model) {
        model.addAttribute("rooms", roomService.list());
        return "room_manage";
    }

    @GetMapping("/")
    @SaCheckLogin
    public String index(Model model) {
        // 查找空房数
        QueryWrapper<Room> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("is_free", 1);  // 空房
        int freeCount = roomService.count(wrapper1);

        // 查找已入住房间数
        QueryWrapper<Room> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("is_free", 0);  // 已入住
        int inCount = roomService.count(wrapper2);

        // 查找已预订房间数
        QueryWrapper<Room> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("is_free", 2);  // 已预订
        int bookCount = roomService.count(wrapper3);

        // 将统计结果放入map
        HashMap<String, Object> map = new HashMap<>();
        map.put("freeCount", freeCount);
        map.put("inCount", inCount);
        map.put("bookCount", bookCount);

        BigDecimal amount=feeRecordService.getTodayAmount();

        map.put("amount",amount);

        // 将map传递给页面
        model.addAttribute("map", map);

        return "index";
    }

    @GetMapping("/logout")
    public String logout() {
        StpUtil.logout();
        return "redirect:/login";
    }

    // 搜索房间 - 返回页面
    @GetMapping("/search")
    public String search(Room room, Model model) {
        List<Room> rooms = roomService.search(room);
        model.addAttribute("rooms", rooms);
        return "room_manage";  // 返回视图
    }
}
